package studio.raptor.extractor.segment.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.locks.LockSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.databus.client.DatabusConfig;
import studio.raptor.databus.client.DatabusProducer;
import studio.raptor.databus.client.DatabusProducerBuilder;
import studio.raptor.databus.command.Event;
import studio.raptor.databus.command.EventBasicData;
import studio.raptor.databus.command.EventBusinessData;
import studio.raptor.databus.command.EventBusinessDataProvideType;
import studio.raptor.databus.command.EventBusinessDataType;
import studio.raptor.databus.command.EventMetaData;
import studio.raptor.databus.command.EventMetaDataBusinessType;

/**
 * Created by liuyong8 on 2017/7/29.
 */
public class EventMQProduterTask implements Runnable{

    private static final Logger log = LoggerFactory.getLogger(EventMQProduterTask.class);
    static SimpleDateFormat Y2S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int count = 100;
    @Override
    public void run() {
        try {
            while (count <101){
                DatabusConfig databusConfig = new DatabusConfig("Extractor_NRM");
                DatabusProducerBuilder producerBuilder = new DatabusProducerBuilder(databusConfig);
                DatabusProducer producer = producerBuilder.create();
                Event event = createEvent(10,1008,"so_order_construct","Extractor_NRM");
                event.getEventMetaData().setBusinessNo("1000000000012124");
                count ++;
                producer.send(event);
                log.info("At "+Y2S.format(new Date())+" send "+event+" success .");
                LockSupport.parkUntil(System.currentTimeMillis() + 10);//休眠0.5秒
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建事件
     * @param eventSize 消息大小
     * @param eventKindId 事件种类ID
     * @param eventName 事件名称
     * @param eventSource 事件来源，即业务中心
     * @return
     */
    private static Event createEvent(int eventSize, Integer eventKindId, String eventName, String eventSource) {
        //设定基础数据
        EventBasicData basicData = new EventBasicData();
        basicData.setSource(eventSource);
        basicData.setKindId(eventKindId);
        basicData.setName(eventName);
        basicData.setDeliverTime(Y2S.format(new Date()));
        //设置业务数据
        EventBusinessData businessData = new EventBusinessData();

        String DATA = "abcdefghijklmnopqrstuvwxyz";
        int KUNIT = 1024;
        StringBuilder sbd = new StringBuilder(eventSize * KUNIT);
        for (int i = 0; i < eventSize * KUNIT; i++) {
            sbd.append(DATA.charAt(new Random().nextInt(26)));
        }
//        businessData.setContent("\"`~!@#%^&*()_+=-;'/,.<>|[]{}【】\\" + sbd.toString() + "\"");
        businessData.setContent("{\"coId\":111}");
        businessData.setDataService("https://www.baidu.com?search=12");
        businessData.setProvideType(EventBusinessDataProvideType.PROVIDE_TYPE_CARRY);
        businessData.setDataType(EventBusinessDataType.DATATYPE_JSON);
        Event event = new Event(basicData, businessData, null);
        EventMetaData metaData = new EventMetaData();
        metaData.setBusinessNo("");
        metaData.setRegionId(8510101L);
        metaData.setBusinessType(EventMetaDataBusinessType.BUSINESS_TYPE_NRM);
        Long[] custId = new Long[]{5101336024194360L};
        metaData.setCustId(custId);
        Long[] prodParam = new Long[]{1000000000007414L};
        metaData.setProdInstId(prodParam);
        Long[] acctId = new Long[]{331044988363L};
        metaData.setAcctId(acctId);
        Long[] offerId = new Long[]{1000000000002753L};
        metaData.setOfferInstId(offerId);
        event.setEventMetaData(metaData);
        return event;
    }
}
